package com.item.mobile;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import sun.misc.BASE64Decoder;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ListObjectsRequest;
import com.aliyun.openservices.oss.model.OSSObjectSummary;
import com.aliyun.openservices.oss.model.ObjectListing;
import com.base.mobile.MobException;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.service.BFileService;
import com.base.util.DateUtil;
import com.base.util.FileExportUtils;
import com.base.util.StringUtil;
import com.google.gson.JsonObject;
import com.item.dao.model.EssenceAuthor;
import com.item.dao.model.EssenceAuthorExample;
import com.item.dao.model.EssenceNew;
import com.item.dao.model.EssenceNewExample;
import com.item.dao.model.MediaFile;
import com.item.dao.model.MediaFileExample;
import com.item.dao.model.News;
import com.item.dao.model.NewsExample;
import com.item.dao.model.Note;
import com.item.dao.model.NoteAuthor;
import com.item.dao.model.NoteExample;
import com.item.dao.model.VersionCheck;
import com.item.dao.model.VersionCheckExample;
import com.item.dao.model.WelcomePage;
import com.item.dao.model.WelcomePageExample;
import com.item.service.EssenceAuthorService;
import com.item.service.EssenceNewService;
import com.item.service.MediaFileService;
import com.item.service.NewsService;
import com.item.service.NoteAuthorService;
import com.item.service.NoteService;
import com.item.service.SubjectDetailService;
import com.item.service.VersionCheckService;
import com.item.service.WelcomePageService;
import com.item.util.MyExcelUtils;
import com.item.util.MyOssClient;
import com.item.util.SubjectUtils;
import com.mobile.api.proto.MAppUser.Mupdateself;
import com.mobile.api.proto.MSystem.MRet;

@Component
@Mobile
public class MAppWelcomePage {
	
	@Autowired
	private WelcomePageService welcomePageService;
	@Autowired
	private VersionCheckService checkService;
	@Autowired
	private BFileService fileService;
	@Autowired
	private MediaFileService fser;
	@Autowired
	private NoteAuthorService authortService;
	@Autowired
	private SubjectDetailService  subDetailService;
	@Autowired
	private NoteService  noteService;
	
	@Autowired
	private NewsService newsService;
	
	@Autowired
	private EssenceNewService  essenceService;
	
	@Autowired
	private EssenceAuthorService  authorService;
	
	private BASE64Decoder decoder = new BASE64Decoder();
	
	
	
	/**
	 * 检查版本更新
	 * @url /mobile?methodno=MVersionCheck&debug=&deviceid=&appid=
	 * @return
	 */
	@MobileMethod(methodno = "MVersionCheck",isLogin=false,isPage=false)
	public Mupdateself.Builder getVersion(MobileInfo mobileInfo)throws Exception{
		Mupdateself.Builder ret = Mupdateself.newBuilder();
		//查询最新的版本
		VersionCheckExample vcex = new VersionCheckExample();
		vcex.setOrderByClause("create_time DESC");
		List<VersionCheck> versions = checkService.selectByExample(vcex);
		
		if(versions != null && versions.size() > 0) {
			VersionCheck version = versions.get(0);
			ret.setId(StringUtil.emptyIfBlank(version.getId()));
			ret.setVersion(version.getVersion());
			ret.setDesc(version.getDescription());
			ret.setUrl(StringUtil.emptyIfBlank(version.getUrl()));
		} else {
			ret.setId("");
			ret.setVersion("");
			ret.setUrl("");
			ret.setDesc("");
		}
		return ret;
	}
	

	@MobileMethod(methodno = "MImgUpload", isLogin = false)
	public MRet.Builder uploadImg(MobileInfo mobileInfo, String img,String filename)throws Exception{
		//校验参数
		if(StringUtils.isBlank(img)){
			throw new MobException("img");
		}
//		System.out.println(img);
//		System.out.println(filename);
		if(StringUtils.isBlank(filename)){
			filename = mobileInfo.getUserid() + ".png";
		}
		//安卓和IOS采取不一样的方法
		String file = fileService.uploadFile(decoder.decodeBuffer(img.replaceAll(" ", "+")), filename, StringUtils.isBlank(mobileInfo.getUserid()) ? "test" : mobileInfo.getUserid());
		if(file == null){
			throw new MobException(MEnumError.UPLOAD_FAILURE_ERROR);
		}
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
//		System.out.println(file);
		ret.setMsg(file);
		return ret;
	}
	
	@MobileMethod(methodno = "Tet", isLogin = false)
	public MRet.Builder test()throws Exception{
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		return ret;
	}
}
